package reggie.ruiji.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import reggie.ruiji.Dto.DishDto;
import reggie.ruiji.common.R;
import reggie.ruiji.pojo.Dish;
import reggie.ruiji.pojo.DishFlavor;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.service.DishFlavorService;
import reggie.ruiji.service.DishService;
import reggie.ruiji.service.SetmealService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

   @Autowired
   private DishFlavorService dishFlavorService;


   @Autowired
   private RedisTemplate redisTemplate;


   @PostMapping
   public R<String> save(@RequestBody DishDto dishDto, HttpServletRequest request)
   {

       dishDto.setCreateTime(LocalDateTime.now());
       dishDto.setUpdateTime(LocalDateTime.now());

       Long empID=(Long)request.getSession().getAttribute("employee");

       dishDto.setCreateUser(empID);
       dishDto.setUpdateUser(empID);
       dishDto.setCode("1");
       dishService.insert(dishDto);
       Long id=dishService.getid(dishDto.getName());
       List<DishFlavor> list=dishDto.getFlavors();

       for(DishFlavor dishFlavor:list)
       {
           dishFlavor.setCreateTime(LocalDateTime.now());
           dishFlavor.setUpdateTime(LocalDateTime.now());
           dishFlavor.setCreateUser(empID);
           dishFlavor.setUpdateUser(empID);
           dishFlavor.setIsDeleted(0);
           dishFlavor.setDishId(id);

       }
       //保存菜品的基本信息到菜品表dish
       //保存菜品口味数据到菜品口味表dish_flavor
       dishFlavorService.save(list);
       //清理某个分类下的菜品缓存数据
       String key="dish"+dishDto.getCategoryId()+"_";
       redisTemplate.delete(key);
       return R.success("新增菜品成功");
   }


    @GetMapping("/page")
    public R<PageBean> getEmps(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize, String name)
    {
        PageBean S=dishService.pagee(page,pageSize,name);
        return R.success(S);
    }

    @GetMapping("/{id}")
    public R<DishDto> getDish(@PathVariable Long id)
    {
        DishDto dishDto=dishService.getDishDtoById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public  R<String> update(@RequestBody DishDto dishDto,HttpServletRequest request)
    {
        dishService.update(dishDto,request);
       /*
       //清理所有菜品的缓存数据
        redisTemplate.delete(redisTemplate.keys("dish*"));
        */

        //清理某个分类下的菜品缓存数据
        String key="dish"+dishDto.getCategoryId()+"_";
        redisTemplate.delete(key);

        return R.success("新增菜品成功");
    }

   @DeleteMapping
    public  R<String> delete(@RequestParam List<Long> ids)
   {

    dishService.delete(ids);
    return R.success("删除成功");
   }


    @PostMapping("/status/{status}")
    public R<String> updateDishStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {



        dishService.updateStatus(ids,status);

        if(status==1)
        {
            return R.success("启售成功");
        }
        else
        {
            return R.success("停售成功");
        }

    }

    /*@GetMapping("/list")
    public R<List<Dish>> getDishByCategoryId(Dish  dish)
    {
       List<Dish>dishs=dishService.getDishByCategoryId(dish);
        return R.success(dishs);
    }*/
    @GetMapping("/list")
    public R<List<DishDto>> getDishByCategoryId(Dish  dish)
    {
        List<DishDto>dishdtos=null;
        String key="dish"+dish.getCategoryId()+"_";
        dishdtos=(List<DishDto>)redisTemplate.opsForValue().get(key);
        //如果Redis中存在，直接从Redis中取数据
        if(dishdtos!=null)
        {

            return R.success(dishdtos);
        }


        //Redis中不存在，需要查询数据库，并将数据放入Redis缓存
        dishdtos=dishService.getDishByCategoryId(dish);
        redisTemplate.opsForValue().set(key,dishdtos,60, TimeUnit.MINUTES);

        return R.success(dishdtos);
    }
/* List<DishDto>dishDtos=null;
        String key="dish"+dish.getCategoryId()+"_"+dish.getStatus();
        dishDtos=(List<DishDto>)redisTemplate.opsForValue().get(key);
        //如果Redis中存在，直接从Redis中取数据
        if(dishDtos!=null)
        {

            return R.success(dishDtos);
        }


        //Redis中不存在，需要查询数据库，并将数据放入Redis缓存0
        DishDto dishDto=dishService.getDishDtoById(dish.getId());
        redisTemplate.opsForValue().set(key,dishDto,60, TimeUnit.MINUTES);

        return R.success(dishDtos);*/




}
